package com.ljt.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.business.entity.InStockInfo;
import com.ljt.business.service.InStockInfoService;
import com.ljt.response.Result;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/bizInStockInfo")
public class InStockInfoController {
    @Autowired
    private InStockInfoService inStockInfoService;

    @ApiOperation(value = "入库订单表",notes = "分页查询入库单信息")
    @GetMapping("/queryInStockInfoByPage")
    public Result queryInStockInfoByPage(@RequestParam(required = false,defaultValue = "1") Integer current,
                                         @RequestParam(required = false,defaultValue = "6") Integer size){
        Page<InStockInfo> inStockInfoPage=new Page<>(current,size);
        inStockInfoPage=inStockInfoService.page(inStockInfoPage);
        return Result.ok().data("inStockInfo",inStockInfoPage);
    }

    @ApiOperation(value = "入库订单表",notes = "修改入库单信息")
    @GetMapping("/updateInStockInfoById")
    public Result updateInStockInfoById(@RequestBody InStockInfo inStockInfo){
        boolean i=inStockInfoService.updateById(inStockInfo);
        if(i){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "入库订单表",notes = "添加入库单信息")
    @GetMapping("/insertInStockInfo")
    public Result insertInStockInfo(@RequestBody InStockInfo inStockInfo){
        boolean i=inStockInfoService.save(inStockInfo);
        if(i){
            return Result.ok().data("inStockInfo",inStockInfo);
        }else{
            return Result.error();
        }
    }

    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("exportInStockInfoToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<InStockInfo> inStockInfo = inStockInfoService.list();
        ExcelUtil<InStockInfo> util = new ExcelUtil<>(InStockInfo.class);
        util.exportExcel(response,inStockInfo , "入库单数据");
    }

    @ApiOperation("导入")
    @PostMapping("importInStockInfoToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<InStockInfo> util = new ExcelUtil<>(InStockInfo.class);
        List<InStockInfo> inStockInfo = util.importExcel(file.getInputStream());
        boolean insertFlag = inStockInfoService.saveBatch(inStockInfo);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}
